# -*- coding: utf-8 -*-
from django.db import models
from datetime import datetime
from basic.substation.conf import *

class Station(models.Model):
    '''变电站'''
    code = models.CharField('变电站编码', max_length=20)
    name = models.CharField('变电站名称', max_length=40)
    abbreviation = models.CharField('变电站简称', max_length=40,
            null=True, blank=True)
    lng = models.FloatField('经度')
    lat = models.FloatField('纬度')    
    voltageGrade = models.CharField('电压等级', choices=STATION_VOLTAGE_GRADE,
            max_length=10)    
    buildDate = models.DateField('建站日期', default=datetime.today())
#设备地位
    equipmentPosition = models.CharField('设备地位', max_length=20,
            null=True, blank=True)
    flag = models.CharField('N-1标志', max_length=20, null=True, blank=True)
    managerOrgnization = models.CharField('管理单位', max_length=40,
            null=True, blank=True)
    summary = models.TextField('变电站简介', null=True, blank=True)
    remarks = models.TextField('备注', null=True, blank=True)
    
    def __unicode__(self):
        return '%s' % self.name

    class Meta:
        verbose_name_plural = verbose_name = '变电信息|变电站(Station)'


class StationEquipment(models.Model):
    code = models.CharField('站内设备编码', max_length=20)
    name = models.CharField('站内设备名称', max_length=40)
    parent = models.ForeignKey('self', verbose_name='父设备',
            related_name='children', null=True, blank=True)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='stationequipments')
    runCode = models.CharField('运行编码', max_length=20,
            null=True, blank=True)
    voltageGrade = models.CharField('电压等级', choices=LINE_VOLTAGE_GRADE,
            max_length=20, null=True, blank=True)
    phaseDifference = models.CharField('相别', max_length=20,
            null=True, blank=True)
    equipmentType = models.CharField('设备类型', max_length=20,
            choices=EQUIPMENT_TYPE, null=True, blank=True)
    equipmentUnit = models.CharField('设备单元', max_length=20,
            choices=EQUIPMENT_UNIT, null=True, blank=True)
    equipmentGroup = models.CharField('设备组', choices=EQUIPMENT_GROUP,
            max_length=20, null=True, blank=True)
    equipmentModel = models.CharField('设备型号', choices=EQUIPMENT_MODEL,
            max_length=20, null=True, blank=True)
    equipmentGrade = models.CharField('设备等级', choices=EQUIPMENT_GRADE,
            max_length=20, null=True, blank=True)
    produceFactory = models.CharField('生产厂家', max_length=20,
            null=True, blank=True)
    leaveFactoryCode = models.CharField('离厂编号', max_length=20,
            null=True, blank=True)
    leaveFactoryDate = models.DateField('离厂日期', default=datetime.now(),
            null=True, blank=True)
    runDate = models.DateField('投运日期', default=datetime.now(),
            null=True, blank=True)
    dispatchRange = models.CharField('调度范围', max_length=20,
            null=True, blank=True)
    runStatus = models.CharField('运行状态', max_length=20,
            null=True, blank=True)
    asset = models.FloatField('资产值(元)', null=True, blank=True)
    networkType = models.CharField('网别', max_length=20,
            choices=NETWORK_TYPE, null=True, blank=True)
    usefulTime = models.FloatField('使用寿命(年)', null=True, blank=True)
    repareCycle = models.FloatField('大修周期(月)', null=True, blank=True)
    lastFixDate = models.DateField('上次修理日期', default=datetime.now(),
            null=True, blank=True)
    summary = models.TextField('站内设备简介', null=True, blank=True)
    
    def __unicode__(self):
        return '%s' % self.name

    class Meta:
        verbose_name_plural = verbose_name = \
        '变电信息|变电站内设备(StationEquipment)'


class UnitCode(models.Model):
    '''单元信息'''
    code = models.CharField('单元编码', max_length=20, unique=True)
    name = models.CharField('单元名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='unitcodes')
    
    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = verbose_name = '变电信息|单元信息(UnitCode)'


class Transformer(models.Model):
    '''变压器'''
    code = models.CharField('变压器编码', max_length=20, unique=True)
    name = models.CharField('变压器名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='transformers')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = '变电信息|变压器(Transformer)'


class CircuitBreaker(models.Model):
    '''断路器'''
    code = models.CharField('断路器编码', max_length=20, unique=True)
    name = models.CharField('断路器名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='circuitbreakers')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = u'变电信息|断路器(CircuitBreaker)'


class Bus(models.Model):
    '''母线'''
    code = models.CharField('母线编码', max_length=20, unique=True)
    name = models.CharField('母线名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='buses')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = u'变电信息|母线(Bus)'


class VoltageTrans(models.Model):
    '''电压互感器'''
    code = models.CharField('电压互感器编码', max_length=20, unique=True)
    name = models.CharField('电压互感器名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='valtagetranses')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = u'变电信息|电压互感器(VoltageTrans)'


class CurrentTrans(models.Model):
    '''电流互感器'''
    code = models.CharField('电流互感器编码', max_length=20, unique=True)
    name = models.CharField('电流互感器名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='currenttranses')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = u'变电信息|电流互感器(CurrentTrans)'


class Arrester(models.Model):
    '''避雷器'''
    code = models.CharField('避雷器编码', max_length=20, unique=True)
    name = models.CharField('避雷器名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='arresters')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = u'变电信息|避雷器(Arrester)'


class WaveTrap(models.Model):
    '''阻波器'''
    code = models.CharField('阻波器编码', max_length=20, unique=True)
    name = models.CharField('阻波器名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='wavetraps')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = u'变电信息|阻波器(WaveTrap)'


class IsolateSwitch(models.Model):
    '''隔离开关'''
    code = models.CharField('隔离开关编码', max_length=20, unique=True)
    name = models.CharField('隔离开关名称', max_length=40)
    station = models.ForeignKey(Station, verbose_name='变电站',
            related_name='isolateswitches')
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name_plural = verbose_name = u'变电信息|阻隔开关(IsolateSwitch)'
